package dev.hitools.common.widget.recyclerview.adapter

import android.util.SparseIntArray
import android.view.ViewGroup
import dev.hitools.common.extensions.inflate
import dev.hitools.common.widget.recyclerview.holder.BindHolder

open class BindAdapter<T> : BaseAdapter<BindHolder, T>() {

    /**
     * item的layout记录
     */
    private val layoutList = SparseIntArray()

    /**
     * data模型数据dataBinding的BR的值
     */
    private val dataVariableList = SparseIntArray()

    override fun createHolder(parent: ViewGroup, viewType: Int): BindHolder {
        val item = parent.inflate(layoutList[viewType])
        return BindHolder(item)
    }

    override fun onBindViewHolder(holder: BindHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        val itemData = getItem(position)
        val viewType = getItemViewType(position)
        holder.bind(dataVariableList[viewType], itemData, position)
    }

    /**
     * 添加布局文件
     */
    @JvmOverloads
    fun addLayout(variableId: Int, layoutRes: Int, viewType: Int = 0) {
        layoutList.put(viewType, layoutRes)
        dataVariableList.put(viewType, variableId)
    }
}

